package me.cxlr.qinlauncher2.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import java.util.Objects;

import me.cxlr.qinlauncher2.manager.SoftwareManager;

/**
 * @author 酷安用户:山轮纸
 */
public class AppReceiver extends BroadcastReceiver {
    public AppReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String packageName;

        switch (Objects.requireNonNull(intent.getAction())) {
            // 安装
            case Intent.ACTION_PACKAGE_ADDED:
                packageName = intent.getDataString().substring(8);
                SoftwareManager.getInstance().addPackage(context, packageName);
                break;
            // 移除
            case Intent.ACTION_PACKAGE_REMOVED:
                packageName = intent.getDataString().substring(8);
                SoftwareManager.getInstance().removePackage(packageName);
                break;
            // 替换
            case Intent.ACTION_PACKAGE_REPLACED:
                packageName = intent.getDataString().substring(8);
                SoftwareManager.getInstance().replacePackage(context, packageName);
                break;
            // 改变
            case Intent.ACTION_PACKAGE_CHANGED:
                packageName = intent.getDataString().substring(8);
                SoftwareManager.getInstance().changePackage(context, packageName);
                break;
            default:
                break;
        }
    }
}
